﻿using System;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;

namespace SHomeWorkshop.LunarPresentation.Commands
{
    public partial class Command
    {
        /// <summary>
        /// 执行文本命令。下划线字符会被自动转换为空格。
        /// </summary>
        /// <param name="cmdString">命令文本。</param>
        /// <returns></returns>
        public static bool RunTextCmd(string cmdString)
        {
            if (cmdString.Contains('_'))
            {
                cmdString = cmdString.Replace('_', ' ');
            }

            Globals.textCmdList.AddCmd(cmdString);
            //将上次使用的命令添加到列表中。

            switch (cmdString)
            {
                case "1"://一、级标题
                    {
                        Globals.SetBaseTextHeaderLevel(3); break;
                    }
                case "2"://㈠级标题
                    {
                        Globals.SetBaseTextHeaderLevel(4); break;
                    }
                case "3"://⒈级标题
                    {
                        Globals.SetBaseTextHeaderLevel(5); break;
                    }
                case "4"://⑴级标题
                    {
                        Globals.SetBaseTextHeaderLevel(6); break;
                    }
                case "0":
                case "zw"://正文
                    {
                        Globals.SetBaseTextHeaderLevel(7); break;
                    }
                case "t1":
                    {
                        Command.InsertHeaderText(1); break;
                    }
                case "t2":
                    {
                        Command.InsertHeaderText(2); break;
                    }
                case "t3":
                    {
                        Command.InsertHeaderText(3); break;
                    }
                case "t4":
                    {
                        Command.InsertHeaderText(4); break;
                    }
                case "ys"://叶面板上移一位。
                    {
                        Command.MoveLeafUp(); break;
                    }
                case "yx"://叶面板下移一位。
                    {
                        Command.MoveLeafDown(); break;
                    }
                case "yz"://叶面板左移一层。
                    {
                        Command.MoveLeafLeft(); break;
                    }
                case "yy"://叶面板右移一层。
                    {
                        Command.MoveLeafRight(); break;
                    }
                case "yc"://选中叶面板所在树的叶首面板。
                case "ysmb":
                case "sgmb":
                    {
                        Command.SelectRootLeafOfATree(); break;
                    }
                case "cl"://插入列（左）
                case "clz":
                    {
                        return Command.InsertColumn(true);
                    }
                case "cly"://插入列（右）
                    {
                        return Command.InsertColumn(false);
                    }
                case "ch"://插入行（上）
                case "chs":
                    {
                        return Command.InsertLine(true);
                    }
                case "chx"://插入行（下）
                    {
                        return Command.InsertLine(false);
                    }
                case "hb"://向右合并
                case "hby":
                    {
                        Command.Merge(Command.MergeOperation.Right); break;
                    }
                case "hbx"://向下合并
                    {
                        Command.Merge(Command.MergeOperation.Bottom); break;
                    }
                case "cf"://取消合并（拆分）
                    {
                        Command.Merge(Command.MergeOperation.Cancel); break;
                    }
                case "sl"://删除列
                    {
                        Command.DeleteColumn(); break;
                    }
                case "sh"://删除行
                    {
                        Command.DeleteLine(); break;
                    }
                case "kg"://插入客观题
                case "kgt":
                    {
                        Command.InsertObjectiveItem(); break;
                    }
                case "zg"://插入主观题
                case "zgt":
                    {
                        Command.InsertSubjectiveItem(); break;
                    }
                case "sj":
                    {
                        if (Command.InsertTriangle() != null)
                            Globals.SelectFirstChildExpression();
                        break;
                    }
                case "jc"://插入交叉关系式
                    {
                        if (Command.InsertLogicCross() != null)
                            Globals.SelectFirstChildExpression();
                        break;
                    }
                case "md"://插入[矛盾/对立]关系式
                    {
                        if (Command.InsertLogicContradiction() != null)
                            Globals.SelectFirstChildExpression();
                        break;
                    }
                case "sz"://插入属种关系式
                    {
                        if (Command.InsertLogicGenuspecies() != null)
                            Globals.SelectFirstChildExpression();
                        break;
                    }
                case "fwbk":
                    {
                        Command.InsertRichText();
                        break;
                    }
                case "gb"://关闭
                    {
                        Globals.MainWindow.Close(); break;
                    }
                case "ss"://选取当前公式内嵌的首个公式。
                    {
                        return Globals.SelectFirstChildExpression();
                    }
                case "tk"://将当前文本块做成填空。
                    {
                        Command.BuildFillBlankExam();
                        break;
                    }
                case "ws"://选取当前公式内嵌的最后一个公式。
                    {
                        return Globals.SelectLastChildExpression();
                    }
                case "bdhz":
                    {
                        Command.SetWhiteBackAndBlackForeground(); break;
                    }
                case "bdcz":
                    {
                        Command.SetWhiteBackAndBlackForeground(true); break;
                    }
                case "hdbz":
                    {
                        Command.SetBlackBackAndWhiteForeground(); break;
                    }
                case "hdcz":
                    {
                        Command.SetBlackBackAndWhiteForeground(true); break;
                    }
                case "hwdhz":
                    {
                        Command.SetTransparentBackAndBlackForeground(); break;
                    }
                case "hwdcz":
                    {
                        Command.SetTransparentBackAndBlackForeground(true); break;
                    }
                case "twg":
                    {
                        Command.PasteExcelDataToGrid();
                        break;
                    }
                case "zkz":
                    {
                        Command.BracketToExpander();
                        break;
                    }
                case "zkh":
                    {
                        Command.ExpanderToBracket();
                        break;
                    }
                case "ymsz":
                    {
                        Command.PageSetting();
                        break;
                    }
                case "dy":
                    {
                        Command.PrintSelectedDocument();
                        break;
                    }
                case "zfwbk":
                    {
                        Command.ConvertRowsToRichText();
                        break;
                    }
                case "lcw":
                    {
                        Command.SaveFileAs(); break;
                    }
                case "dcwd":
                    {
                        Command.OutportDocumentToImage(); break;
                    }
                case "dcgs":
                    {
                        Command.OutportExpressionToImage(); break;
                    }
                case "?":
                    {
                        Command.Help(); break;
                    }
                case "help":
                    {
                        //弹出关于命令模式的帮助
                        ExContextMenus.ShowHelpFile("cmdsandshortcuts.htm");
                        break;
                    }
                case "zzzq"://总在最前。
                    {
                        Globals.MainWindow.Topmost = !Globals.MainWindow.Topmost;
                        Globals.MainWindow.tbtnTopMost.IsChecked = Globals.MainWindow.Topmost;
                        break;
                    }
                case "djbt":
                    {
                        //顶级标题。
                        if (Globals.MainWindow.cbTextHeaderLevel.IsEnabled)
                        {
                            Globals.MainWindow.cbTextHeaderLevel.SelectedIndex = 0;
                        }
                        break;
                    }
                case "djfbt":
                    {
                        //顶级副标题。
                        if (Globals.MainWindow.cbTextHeaderLevel.IsEnabled)
                        {
                            Globals.MainWindow.cbTextHeaderLevel.SelectedIndex = 1;
                        }
                        break;
                    }
                case "ztps":
                    {
                        //将当前选中的公式指定为通配式。
                        Command.AssignWildcardExpression();
                        break;
                    }
                case "rwzb":
                    {
                        Command.SetTaskItemType(Expressions.BaseText.TaskType.NotStarted); break;
                    }
                case "rwyx":
                    {
                        Command.SetTaskItemType(Expressions.BaseText.TaskType.Running); break;
                    }
                case "rwwc":
                    {
                        Command.SetTaskItemType(Expressions.BaseText.TaskType.Finished); break;
                    }
                case "rwsb":
                    {
                        Command.SetTaskItemType(Expressions.BaseText.TaskType.Aborted); break;
                    }
                case "rwzt":
                    {
                        Command.SetTaskItemType(Expressions.BaseText.TaskType.Paused); break;
                    }
                case "rwqx":
                    {
                        Command.SetTaskItemType(Expressions.BaseText.TaskType.NotTask); break;
                    }
                case "yxx":
                    {
                        Command.BaseTextMaskRightBias(); break;
                    }
                case "hx":
                    {
                        Command.BaseTextMaskHLine(); break;
                    }
                case "sx":
                    {
                        Command.BaseTextMaskVLine(); break;
                    }
                case "wgx":
                    {
                        Command.BaseTextMaskHVLine(); break;
                    }
                case "wzz":
                    {
                        Command.BaseTextMaskNone(); break;
                    }
                case "xml":
                    {
                        if (Globals.selectedEditor != null &&
                            Globals.selectedEditor.SelectedExpression != null &&
                            Globals.selectedEditor.SelectedExpression.XmlData != null)
                        {

                            MessageBox.Show("ExID:" + Globals.selectedEditor.SelectedExpression.ExID +
                                "\r\n\r\nXmlData:\r\n\r\n" +
                                Globals.selectedEditor.SelectedExpression.XmlData.OuterXml.ToString());
                        }
                        break;
                    }
                case "sxx"://切换标题文本块显示树型连线
                    {
                        Command.SwitchTreeLine(); break;
                    }
                case "sxxqk":
                case "qksxx"://开启当前子面板中所有标题文本块的树型线绘制功能。
                    {
                        Command.ShowAllTreeLineOfASubPanel(); break;
                    }
                case "sxxqg":
                case "qgsxx"://关闭当前子面板中所有标题文本块的树型线绘制功能。
                    {
                        Command.HideAllTreeLineOfASubPanel(); break;
                    }
                case "zmrps"://将当前文档的配色方案设置为下次启动程序后新建文档的默认配色方案。
                    {
                        Command.SaveDocColorSolution(); break;
                    }
                case "qcmrps"://清除当前使用的默认配色方案。
                    {
                        Command.ClearDocColorSolution(); break;
                    }
                case "ym"://设置文档页眉。
                    {
                        Command.EditDocPageHeader(); break;
                    }
                case "bzdbh":
                    {
                        Command.SwitchTrySemiAutoNum(); break;
                    }
                #region “竖排文本”和“横排文本”这两个功能并不是正好互斥的——不能保证结果返回原样。
                case "spwb"://竖排文本
                case "wbsp"://文本竖排
                case "mzfhh"://令一个文本块按字符换行——在每一个普通字符后添加\r\n
                    {
                        if (Globals.selectedEditor == null ||
                            Globals.selectedEditor.SelectedExpression == null)
                        {
                            MessageBox.Show("请选中一个文本块！", Globals.appName,
                                MessageBoxButton.OK, MessageBoxImage.Warning);
                            return false;
                        }

                        Expressions.BaseText bt = Globals.selectedEditor.SelectedExpression as Expressions.BaseText;
                        if (bt == null)
                        {
                            MessageBox.Show("请选中一个文本块！", Globals.appName,
                                MessageBoxButton.OK, MessageBoxImage.Warning);
                            return false;
                        }

                        if (bt.Text.Length > 20)
                        {
                            MessageBox.Show("这个文本块的文本长度超过２０，不宜使用此功能！" +
                                "——这会使文本块过高，没什么意义。", Globals.appName,
                                MessageBoxButton.OK, MessageBoxImage.Warning);
                            return false;
                        }

                        string source = bt.Text.Replace("\r", "");
                        source = source.Replace("\n", "");

                        StringBuilder sb = new StringBuilder();
                        foreach (char c in source)
                        {
                            sb.Append(c); sb.Append("\r\n");
                        }

                        string resultStr = sb.ToString();

                        resultStr = resultStr.Substring(0, resultStr.Length - 2);//去除最后的\r\n

                        ModifingItem mi = new ModifingItem();

                        Action actText = new Action(bt.ExID, -1, bt.GetType().Name, "Text",
                            bt.Text, resultStr);

                        bt.Text = resultStr;

                        mi.Add(actText);
                        bt.Editor.Do(mi);
                        bt.Editor.IsModified = true;

                        return true;
                    }
                case "hpwb"://横排文本
                case "wbhp"://文本横排
                case "schh"://删除换行
                    {
                        if (Globals.selectedEditor == null ||
                            Globals.selectedEditor.SelectedExpression == null)
                        {
                            MessageBox.Show("请选中一个文本块！", Globals.appName,
                                MessageBoxButton.OK, MessageBoxImage.Warning);
                            return false;
                        }

                        Expressions.BaseText bt = Globals.selectedEditor.SelectedExpression as Expressions.BaseText;
                        if (bt == null)
                        {
                            MessageBox.Show("请选中一个文本块！", Globals.appName,
                                MessageBoxButton.OK, MessageBoxImage.Warning);
                            return false;
                        }

                        string resultStr = bt.Text.Replace("\r", "");
                        resultStr = resultStr.Replace("\n", "");

                        ModifingItem mi = new ModifingItem();

                        Action actText = new Action(bt.ExID, -1, bt.GetType().Name, "Text",
                            bt.Text, resultStr);

                        bt.Text = resultStr;

                        mi.Add(actText);
                        bt.Editor.Do(mi);
                        bt.Editor.IsModified = true;

                        return true;
                    }
                #endregion
                case "tqwb":
                    {
                        if (Globals.selectedEditor != null && Globals.selectedEditor.SelectedExpression != null)
                        {
                            System.Windows.Clipboard.SetData(DataFormats.UnicodeText,
                                Globals.selectedEditor.SelectedExpression.XmlData.InnerText);
                            MessageBox.Show("已提取文本到剪贴板！",
                                Globals.appName, MessageBoxButton.OK, MessageBoxImage.Information);
                        }
                        break;
                    }
                default:
                    {
                        bool isComplexCmd = RunComplexComd(cmdString);

                        //if (isComplexCmd == false)
                        //{
                        //    MessageBox.Show("无此命令！", Globals.appName,
                        //        MessageBoxButton.OK, MessageBoxImage.Warning);
                        //}

                        return isComplexCmd;
                    }
            }

            return true;
        }

        private static bool RunComplexComd(string cmdString)
        {
            if (cmdString.StartsWith("chx "))
            {
                string linesCountString = cmdString.Substring(3);
                try
                {
                    if (Globals.selectedEditor == null)
                    {
                        MessageBox.Show("当前未打开任何文档！", Globals.appName,
                            MessageBoxButton.OK, MessageBoxImage.Error);
                        return false;
                    }

                    if (Globals.selectedEditor.SelectedExpression == null)
                    {
                        MessageBox.Show("当前文档没有选中任何公式！", Globals.appName,
                            MessageBoxButton.OK, MessageBoxImage.Error);
                        return false;
                    }

                    if (Globals.selectedEditor.SelectedExpression is Expressions.GridPanel == false)
                    {
                        MessageBox.Show("请先选中一个网格子面板，然后再执行此命令！", Globals.appName,
                            MessageBoxButton.OK, MessageBoxImage.Error);
                        return false;
                    }

                    int linesCount = int.Parse(linesCountString);

                    if (linesCount < 1)
                    {
                        MessageBox.Show("输入的命令无意义！", Globals.appName,
                            MessageBoxButton.OK, MessageBoxImage.Error);
                        return false;
                    }
                    else if (linesCount > 10)
                    {
                        MessageBox.Show("网格式比较耗资源，因此禁止一次插入超过10行！请尽可能不要让网格式过于复杂。", Globals.appName,
                            MessageBoxButton.OK, MessageBoxImage.Error);
                        return false;
                    }

                    for (int i = 1; i <= linesCount; i++)
                    {
                        Command.InsertLine(false, true);
                    }
                    return true;
                }
                catch (Exception e)
                {
                    Dialogs.LunarMsgBox.MsgBoxShow(
                        Globals.appName,
                        "输入的命令格式错误！",
                        "异常消息如下：\r\n" + e.Message,
                        e.StackTrace,
                        MessageBoxButton.OK,
                        MessageBoxImage.Error);
                    return false;
                }
            }
            else if (cmdString.StartsWith("cly "))
            {
                if (Globals.selectedEditor == null)
                {
                    MessageBox.Show("当前未打开任何文档！", Globals.appName,
                        MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                if (Globals.selectedEditor.SelectedExpression == null)
                {
                    MessageBox.Show("当前文档没有选中任何公式！", Globals.appName,
                        MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                if (Globals.selectedEditor.SelectedExpression is Expressions.GridPanel == false)
                {
                    MessageBox.Show("请先选中一个网格子面板，然后再执行此命令！", Globals.appName,
                        MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                string columnsCountString = cmdString.Substring(3);
                try
                {
                    int columnsCount = int.Parse(columnsCountString);

                    if (columnsCount < 1)
                    {
                        MessageBox.Show("输入的命令无意义！", Globals.appName,
                            MessageBoxButton.OK, MessageBoxImage.Error);
                        return false;
                    }
                    else if (columnsCount > 10)
                    {
                        MessageBox.Show("网格式比较耗资源，因此禁止一次插入超过10行！请尽可能不要让网格式过于复杂。", Globals.appName,
                            MessageBoxButton.OK, MessageBoxImage.Error);
                        return false;
                    }

                    for (int i = 1; i <= columnsCount; i++)
                    {
                        Command.InsertColumn(false);
                    }
                    return true;
                }
                catch (Exception e)
                {
                    Dialogs.LunarMsgBox.MsgBoxShow(
                        Globals.appName,
                        "输入的命令格式错误！",
                        "异常消息如下：\r\n" + e.Message,
                        e.StackTrace,
                        MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }
            }
            else if (cmdString.StartsWith("wbxr "))
            {
                string subCmdstring = cmdString.Substring(5);
                if (subCmdstring == null || subCmdstring.Length <= 0)
                {
                    MessageBox.Show("命令文本中未指定渲染方式，无法执行。", Globals.appName,
                        MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                switch (subCmdstring)
                {
                    case "rl":
                        {
                            TextOptions.SetTextFormattingMode(Globals.MainWindow, TextFormattingMode.Display);
                            return true;
                        }
                    case "rh":
                        {
                            TextOptions.SetTextFormattingMode(Globals.MainWindow, TextFormattingMode.Ideal);
                            return true;
                        }
                    default:
                        {
                            MessageBox.Show("命令文本中未指定渲染方式无效，无法执行。" +
                            "（只支持“rl”<锐利>和“rh”<柔和>。", Globals.appName,
                                 MessageBoxButton.OK, MessageBoxImage.Error);
                            return false;
                        }
                }
            }
            else if (cmdString.StartsWith("mrbjs "))
            {
                #region 设置文档默认背景色
                if (Globals.selectedEditor == null)
                {
                    MessageBox.Show("未打开任何文档。", Globals.appName,
                       MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                string subCmdstring = cmdString.Substring(6);

                if (subCmdstring == null || subCmdstring.Length <= 0)
                {
                    MessageBox.Show("命令文本中未指定要设置的色彩名称。", Globals.appName,
                       MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                Brush newBrush = ColorPicker.GetBrushByName(subCmdstring);
                if (newBrush == null)
                {
                    MessageBox.Show("命令文本中指定的色彩名称无效。请注意色彩名称总是首字母大写。", Globals.appName,
                       MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                ModifingItem mi = new ModifingItem();
                Action action;

                if (Globals.selectedEditor.DefaultBackground is ImageBrush)
                {
                    action = new Action(null, -1, null, null,
                       Globals.selectedEditor.DefaultBackgroundXmlString,
                       subCmdstring, ActionType.DefaultExBackgroundChanging, null);
                }
                else
                {
                    action = new Action(null, -1, null, null,
                       ColorPicker.GetBrushName(Globals.selectedEditor.DefaultBackground),
                       subCmdstring, ActionType.DefaultExBackgroundChanging, null);
                }

                Globals.selectedEditor.DefaultBackground = newBrush;
                Globals.selectedEditor.IsModified = true;

                mi.Add(action);
                Globals.selectedEditor.Do(mi);
                return true;
                #endregion
            }
            else if (cmdString.StartsWith("mrqjs "))
            {
                #region 设置文档默认前景色
                if (Globals.selectedEditor == null)
                {
                    MessageBox.Show("未打开任何文档。", Globals.appName,
                       MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                string subCmdstring = cmdString.Substring(6);

                if (subCmdstring == null || subCmdstring.Length <= 0)
                {
                    MessageBox.Show("命令文本中未指定要设置的色彩名称。", Globals.appName,
                      MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                Brush newBrush = ColorPicker.GetBrushByName(subCmdstring);
                if (newBrush == null)
                {
                    MessageBox.Show("命令文本中指定的色彩名称无效。请注意色彩名称总是首字母大写。", Globals.appName,
                       MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                ModifingItem mi = new ModifingItem();
                Action action = new Action(null, -1, null, null,
                   ColorPicker.GetBrushName(Globals.selectedEditor.DefaultForeground),
                   subCmdstring, ActionType.DefaultExForegroundChanging, null);

                Globals.selectedEditor.DefaultForeground = newBrush;
                Globals.selectedEditor.IsModified = true;

                mi.Add(action);
                Globals.selectedEditor.Do(mi);
                return true;
                #endregion
            }
            else if (cmdString.StartsWith("xks1 "))
            {
                #region 设置选框１色彩
                if (Globals.selectedEditor == null)
                {
                    MessageBox.Show("未打开任何文档。", Globals.appName,
                       MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                string subCmdstring = cmdString.Substring(5);

                if (subCmdstring == null || subCmdstring.Length <= 0)
                {
                    MessageBox.Show("命令文本中未指定要设置的色彩名称。", Globals.appName,
                      MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                Brush newBrush = ColorPicker.GetBrushByName(subCmdstring);
                if (newBrush == null)
                {
                    MessageBox.Show("命令文本中指定的色彩名称无效。请注意色彩名称总是首字母大写。", Globals.appName,
                       MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                ModifingItem mi = new ModifingItem();

                if (Globals.selectedEditor != null &&
                    Globals.selectedEditor.SelectedExpression != null)
                {
                    mi.OldSelectedExID = Globals.selectedEditor.SelectedExpression.ExID;
                }

                Action action = new Action(null, -1, null, null,
                   ColorPicker.GetBrushName(Globals.selectedEditor.ExSelectionAdornerBrush),
                   subCmdstring, ActionType.ExSelectionAdornerBrushChanging, null);

                Globals.selectedEditor.ExSelectionAdornerBrush = newBrush;
                Globals.selectedEditor.IsModified = true;

                mi.Add(action);
                Globals.selectedEditor.Do(mi);
                return true;
                #endregion
            }
            else if (cmdString.StartsWith("xks2 "))
            {
                #region 设置选框２色彩
                if (Globals.selectedEditor == null)
                {
                    MessageBox.Show("未打开任何文档。", Globals.appName,
                       MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                string subCmdstring = cmdString.Substring(5);

                if (subCmdstring == null || subCmdstring.Length <= 0)
                {
                    MessageBox.Show("命令文本中未指定要设置的色彩名称。", Globals.appName,
                     MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                Brush newBrush = ColorPicker.GetBrushByName(subCmdstring);
                if (newBrush == null)
                {
                    MessageBox.Show("命令文本中指定的色彩名称无效。请注意色彩名称总是首字母大写。", Globals.appName,
                       MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                ModifingItem mi = new ModifingItem();

                if (Globals.selectedEditor != null &&
                    Globals.selectedEditor.SelectedExpression != null)
                {
                    mi.OldSelectedExID = Globals.selectedEditor.SelectedExpression.ExID;
                }

                Action action = new Action(null, -1, null, null,
                   ColorPicker.GetBrushName(Globals.selectedEditor.SubPanelSelectionAdornerBrush),
                   subCmdstring, ActionType.SubPanelSelectionAdornerBrushChanging,
                   null);

                Globals.selectedEditor.SubPanelSelectionAdornerBrush = newBrush;
                Globals.selectedEditor.IsModified = true;

                mi.Add(action);
                Globals.selectedEditor.Do(mi);
                return true;
                #endregion
            }
            else if (cmdString.StartsWith("1jbts "))
            {
                #region 1级标题默认色彩
                if (Globals.selectedEditor == null)
                {
                    MessageBox.Show("未打开任何文档。", Globals.appName,
                       MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                string subCmdstring = cmdString.Substring(6);

                if (subCmdstring == null || subCmdstring.Length <= 0)
                {
                    MessageBox.Show("命令文本中未指定要设置的色彩名称。", Globals.appName,
                      MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                Brush newBrush = ColorPicker.GetBrushByName(subCmdstring);
                if (newBrush == null)
                {
                    MessageBox.Show("命令文本中指定的色彩名称无效。请注意色彩名称总是首字母大写。", Globals.appName,
                       MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                ModifingItem mi = new ModifingItem();
                Action action = new Action(null, -1, null, null,
                   ColorPicker.GetBrushName(Globals.selectedEditor.DefHeaderText1Brush),
                   subCmdstring, ActionType.DefaultHeaderText1ColorChanging, null);

                Globals.selectedEditor.DefHeaderText1Brush = newBrush;
                Globals.selectedEditor.IsModified = true;

                mi.Add(action);
                Globals.selectedEditor.Do(mi);
                return true;
                #endregion
            }
            else if (cmdString.StartsWith("2jbts "))
            {
                #region 2级标题默认色彩
                if (Globals.selectedEditor == null)
                {
                    MessageBox.Show("未打开任何文档。", Globals.appName,
                       MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                string subCmdstring = cmdString.Substring(6);

                if (subCmdstring == null || subCmdstring.Length <= 0)
                {
                    MessageBox.Show("命令文本中未指定要设置的色彩名称。", Globals.appName,
                      MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                Brush newBrush = ColorPicker.GetBrushByName(subCmdstring);
                if (newBrush == null)
                {
                    MessageBox.Show("命令文本中指定的色彩名称无效。请注意色彩名称总是首字母大写。", Globals.appName,
                       MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                ModifingItem mi = new ModifingItem();
                Action action = new Action(null, -1, null, null,
                   ColorPicker.GetBrushName(Globals.selectedEditor.DefHeaderText2Brush),
                   subCmdstring, ActionType.DefaultHeaderText2ColorChanging, null);

                Globals.selectedEditor.DefHeaderText2Brush = newBrush;
                Globals.selectedEditor.IsModified = true;

                mi.Add(action);
                Globals.selectedEditor.Do(mi);
                return true;
                #endregion
            }
            else if (cmdString.StartsWith("3jbts "))
            {
                #region 3级标题默认色彩
                if (Globals.selectedEditor == null)
                {
                    MessageBox.Show("未打开任何文档。", Globals.appName,
                       MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                string subCmdstring = cmdString.Substring(6);

                if (subCmdstring == null || subCmdstring.Length <= 0)
                {
                    MessageBox.Show("命令文本中未指定要设置的色彩名称。", Globals.appName,
                      MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                Brush newBrush = ColorPicker.GetBrushByName(subCmdstring);
                if (newBrush == null)
                {
                    MessageBox.Show("命令文本中指定的色彩名称无效。请注意色彩名称总是首字母大写。", Globals.appName,
                       MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                ModifingItem mi = new ModifingItem();
                Action action = new Action(null, -1, null, null,
                   ColorPicker.GetBrushName(Globals.selectedEditor.DefHeaderText3Brush),
                   subCmdstring, ActionType.DefaultHeaderText3ColorChanging, null);

                Globals.selectedEditor.DefHeaderText3Brush = newBrush;
                Globals.selectedEditor.IsModified = true;

                mi.Add(action);
                Globals.selectedEditor.Do(mi);
                return true;
                #endregion
            }
            else if (cmdString.StartsWith("4jbts "))
            {
                #region 4级标题默认色彩
                if (Globals.selectedEditor == null)
                {
                    MessageBox.Show("未打开任何文档。", Globals.appName,
                       MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                string subCmdstring = cmdString.Substring(6);

                if (subCmdstring == null || subCmdstring.Length <= 0)
                {
                    MessageBox.Show("命令文本中未指定要设置的色彩名称。", Globals.appName,
                      MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                Brush newBrush = ColorPicker.GetBrushByName(subCmdstring);
                if (newBrush == null)
                {
                    MessageBox.Show("命令文本中指定的色彩名称无效。请注意色彩名称总是首字母大写。", Globals.appName,
                       MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                ModifingItem mi = new ModifingItem();
                Action action = new Action(null, -1, null, null,
                   ColorPicker.GetBrushName(Globals.selectedEditor.DefHeaderText4Brush),
                   subCmdstring, ActionType.DefaultHeaderText4ColorChanging, null);

                Globals.selectedEditor.DefHeaderText4Brush = newBrush;
                Globals.selectedEditor.IsModified = true;

                mi.Add(action);
                Globals.selectedEditor.Do(mi);
                return true;
                #endregion
            }
            else if (cmdString.StartsWith("bkk "))
            {
                #region 设置边框宽度
                if (cmdString.Length <= 4)
                {
                    MessageBox.Show("命令文本中未指定边框宽度。", Globals.appName,
                       MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                string width = cmdString.Substring(4);
                try
                {
                    int newWidth = int.Parse(width);
                    if (newWidth < 0 || newWidth > 20)
                    {
                        MessageBox.Show("公式边框宽度取值只能在０－２０之间。",
                        Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                        return false;
                    }
                    else
                    {
                        if (Globals.selectedEditor == null)
                        {
                            MessageBox.Show("未打开任何文档。", Globals.appName,
                                 MessageBoxButton.OK, MessageBoxImage.Error);
                            return false;
                        }

                        if (Globals.selectedEditor.SelectedExpression == null)
                        {
                            MessageBox.Show("请先选中要设置边框宽度的公式！",
                                Globals.appName, MessageBoxButton.OK,
                                MessageBoxImage.Warning);
                            return false;
                        }
                        else
                        {
                            Expressions.Expression curEx = Globals.selectedEditor.SelectedExpression;
                            ModifingItem mi = new ModifingItem();
                            Action actBorderWidth = new Action(curEx.ExID, -1,
                                curEx.GetType().Name, "BorderWidth",
                                curEx.BorderWidth.ToString(), newWidth.ToString());

                            curEx.BorderWidth = newWidth;

                            curEx.Editor.Do(mi);
                            curEx.Editor.IsModified = true;
                            return true;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Dialogs.LunarMsgBox.MsgBoxShow(
                        Globals.appName,
                        "设置边框宽度命令有异常！",
                        "异常信息如下：\r\n" + ex.Message,
                        ex.StackTrace, MessageBoxButton.OK, MessageBoxImage.Warning);
                    return false;
                }
                #endregion
            }
            else if (cmdString.StartsWith("qjs "))
            {
                #region 公式前景色
                if (Globals.selectedEditor == null)
                {
                    MessageBox.Show("未打开任何文档。", Globals.appName,
                       MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                Expressions.Expression curEx = Globals.selectedEditor.SelectedExpression;
                if (curEx == null)
                {
                    MessageBox.Show("请先选中一个公式。", Globals.appName,
                       MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                string subCmdstring = cmdString.Substring(4);

                if (subCmdstring == null || subCmdstring.Length <= 0)
                {
                    MessageBox.Show("命令文本中未指定要设置的色彩名称。", Globals.appName,
                      MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                Brush newBrush = ColorPicker.GetBrushByName(subCmdstring);
                if (newBrush == null)
                {
                    MessageBox.Show("命令文本中指定的色彩名称无效。请注意色彩名称总是首字母大写。", Globals.appName,
                       MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                ModifingItem mi = new ModifingItem();
                Action action = new Action(curEx.ExID, -1, curEx.GetType().Name, "ExForeground",
                   ColorPicker.GetBrushName(curEx.ExForeground), subCmdstring);

                curEx.ExForeground = newBrush;
                Globals.selectedEditor.IsModified = true;

                mi.Add(action);
                Globals.selectedEditor.Do(mi);
                return true;
                #endregion
            }
            else if (cmdString.StartsWith("bjs "))
            {
                #region 公式背景色
                if (Globals.selectedEditor == null)
                {
                    MessageBox.Show("未打开任何文档。", Globals.appName,
                       MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                Expressions.Expression curEx = Globals.selectedEditor.SelectedExpression;
                if (curEx == null)
                {
                    MessageBox.Show("请先选中一个公式。", Globals.appName,
                       MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                string subCmdstring = cmdString.Substring(4);

                if (subCmdstring == null || subCmdstring.Length <= 0)
                {
                    MessageBox.Show("命令文本中未指定要设置的色彩名称。", Globals.appName,
                      MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                Brush newBrush = ColorPicker.GetBrushByName(subCmdstring);
                if (newBrush == null)
                {
                    MessageBox.Show("命令文本中指定的色彩名称无效。请注意色彩名称总是首字母大写。", Globals.appName,
                       MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                ModifingItem mi = new ModifingItem();
                Action action = new Action(curEx.ExID, -1, curEx.GetType().Name, "ExBackground",
                   ColorPicker.GetBrushName(curEx.ExBackground), subCmdstring);

                curEx.ExBackground = newBrush;
                Globals.selectedEditor.IsModified = true;

                mi.Add(action);
                Globals.selectedEditor.Do(mi);
                return true;
                #endregion
            }
            else if (cmdString.StartsWith("bks "))
            {
                #region 公式边框色
                if (Globals.selectedEditor == null)
                {
                    MessageBox.Show("未打开任何文档。", Globals.appName,
                       MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                Expressions.Expression curEx = Globals.selectedEditor.SelectedExpression;
                if (curEx == null)
                {
                    MessageBox.Show("请先选中一个公式。", Globals.appName,
                       MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                string subCmdstring = cmdString.Substring(4);

                if (subCmdstring == null || subCmdstring.Length <= 0)
                {
                    MessageBox.Show("命令文本中未指定要设置的色彩名称。", Globals.appName,
                      MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                Brush newBrush = ColorPicker.GetBrushByName(subCmdstring);
                if (newBrush == null)
                {
                    MessageBox.Show("命令文本中指定的色彩名称无效。请注意色彩名称总是首字母大写。", Globals.appName,
                       MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                ModifingItem mi = new ModifingItem();
                Action action = new Action(curEx.ExID, -1, curEx.GetType().Name, "BorderColor",
                   ColorPicker.GetBrushName(curEx.BorderColor), subCmdstring);

                curEx.BorderColor = newBrush;
                Globals.selectedEditor.IsModified = true;

                mi.Add(action);
                Globals.selectedEditor.Do(mi);
                return true;
                #endregion
            }
            else if (cmdString.StartsWith("zdzh"))
            {
                #region 设置“是否自动折行”
                if (Globals.selectedEditor == null)
                {
                    MessageBox.Show("未打开任何文档。", Globals.appName,
                        MessageBoxButton.OK, MessageBoxImage.Warning);
                    return false;
                }

                bool newValue = false;
                if (cmdString == "zdzh" || cmdString == "zdzh k")
                {
                    newValue = true;
                }
                else if (cmdString == "zdzh g")
                {
                    newValue = false;
                }

                if (newValue == Globals.selectedEditor.AutoWrap)
                {
                    return true;
                }

                ModifingItem mi = new ModifingItem();
                Action action = new Action(null, -1, null, null,
                    Globals.selectedEditor.AutoWrap.ToString(),
                   newValue.ToString(), ActionType.AutoWrap, null);

                Globals.selectedEditor.AutoWrap = newValue;
                Globals.selectedEditor.IsModified = true;

                mi.Add(action);
                Globals.selectedEditor.Do(mi);
                return true;
                #endregion
            }
            else if (cmdString.StartsWith("/"))
            {
                if (cmdString.StartsWith("//"))//设置为公式模板。
                {
                    return Command.ExTemplateAdd(cmdString.Substring(2));
                }
                else
                {
                    return Command.ExTemplateReadAndInsert(cmdString.Substring(1));
                }
            }
            else if (cmdString.StartsWith("zdwbk "))
            {
                #region 设置最大文本块宽度
                if (cmdString.Length <= 6)
                {
                    MessageBox.Show("命令文本中未指定最大文本宽度。", Globals.appName,
                       MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                string width = cmdString.Substring(6);
                try
                {
                    double newWidth = double.Parse(width);
                    if (newWidth < 200 || newWidth > 3200)
                    {
                        MessageBox.Show("公式边框宽度取值只能在200－3200之间。",
                        Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                        return false;
                    }
                    else
                    {
                        if (Globals.selectedEditor == null)
                        {
                            MessageBox.Show("未打开任何文档。", Globals.appName,
                                 MessageBoxButton.OK, MessageBoxImage.Error);
                            return false;
                        }

                        if (Globals.selectedEditor.AutoWrap == false)
                        {
                            MessageBox.Show("当前文档的“文本自动折行”功能被关闭，此时设置“最大文本宽度”无意义。",
                                Globals.appName, MessageBoxButton.OK, MessageBoxImage.Error);
                            return false;
                        }

                        ModifingItem mi = new ModifingItem();
                        Action actMaxTextWidth = new Action(null, -1,
                            null, null, Globals.selectedEditor.MaxTextWidth.ToString(),
                            newWidth.ToString(), ActionType.MaxTextWidth);

                        Globals.selectedEditor.MaxTextWidth = newWidth;

                        mi.Add(actMaxTextWidth);
                        Globals.selectedEditor.Do(mi);
                        Globals.selectedEditor.IsModified = true;
                        return true;
                    }
                }
                catch (Exception ex)
                {
                    Dialogs.LunarMsgBox.MsgBoxShow(
                        Globals.appName,
                        "设置文本块最大宽度命令有异常！",
                        "异常信息如下：\r\n" + ex.Message,
                        ex.StackTrace, MessageBoxButton.OK, MessageBoxImage.Warning);
                    return false;
                }
                #endregion
            }
            else if (cmdString.StartsWith("zdymk "))
            {
                #region 设置最大根面板的最大宽度
                if (cmdString.Length <= 6)
                {
                    MessageBox.Show("命令文本中未指定最大页面宽度。", Globals.appName,
                       MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                string width = cmdString.Substring(6);
                try
                {
                    if (width.Trim().ToLower() != "max")
                    {
                        PaperInfo pi = PaperInfo.GetPaperInfo(width);
                        if (pi == null)
                        {
                            MessageBox.Show("　　命令文本中指定最大页面宽度必须是某种纸型的英文名称。（例如：zdymk_A4）\r\n" +
                            "　　如果要取消最大页面宽度限制，请使用：zkymk_Max",
                                Globals.appName, MessageBoxButton.OK, MessageBoxImage.Error);
                            return false;
                        }
                    }

                    if (Globals.selectedEditor == null)
                    {
                        MessageBox.Show("未打开任何文档。", Globals.appName,
                             MessageBoxButton.OK, MessageBoxImage.Error);
                        return false;
                    }

                    ModifingItem mi = new ModifingItem();
                    Action actMaxWidthOfRootPanel = new Action(null, -1,
                        null, null, Globals.selectedEditor.MaxWidthOfRootPanel.ToString(),
                        width.ToString(), ActionType.MaxWidthOfRootPanel);

                    Globals.selectedEditor.MaxWidthOfRootPanel = width;

                    mi.Add(actMaxWidthOfRootPanel);
                    Globals.selectedEditor.Do(mi);
                    Globals.selectedEditor.IsModified = true;
                    return true;
                }
                catch (Exception ex)
                {
                    Dialogs.LunarMsgBox.MsgBoxShow(
                        Globals.appName,
                        "设置根面板最大宽度命令有异常！",
                        "异常信息如下：\r\n" + ex.Message,
                        ex.StackTrace, MessageBoxButton.OK, MessageBoxImage.Warning);
                    return false;
                }
                #endregion
            }
            else if (cmdString.StartsWith("xz "))
            {
                if (Globals.selectedEditor == null)
                {
                    MessageBox.Show("没有打开任何文档。", Globals.appName,
                       MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                Expressions.Expression curEx = Globals.selectedEditor.SelectedExpression;

                if (curEx == null)
                {
                    MessageBox.Show("没有选中任何公式。", Globals.appName,
                       MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                if (cmdString.Length <= 3)
                {
                    MessageBox.Show("命令文本中未指定旋转角度。", Globals.appName,
                       MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                string rotate = cmdString.Substring(3);
                try
                {
                    double newRotate = double.Parse(rotate);
                    if (newRotate < -180 || newRotate > 180)
                    {
                        MessageBox.Show("公式旋转角度取值只能在[-180,180]之间。",
                        Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                        return false;
                    }

                    ModifingItem mi = new ModifingItem();
                    Action actRotate = new Action(curEx.ExID, -1,
                      curEx.GetType().Name, "RotateNumber", curEx.RotateNumber.ToString(), rotate);

                    curEx.RotateNumber = newRotate;

                    mi.Add(actRotate);
                    Globals.selectedEditor.Do(mi);
                    Globals.selectedEditor.IsModified = true;
                    return true;
                }
                catch (Exception ex)
                {
                    Dialogs.LunarMsgBox.MsgBoxShow(
                       Globals.appName,
                       "设置旋转角度命令有异常！",
                       "异常信息如下：\r\n" + ex.Message,
                       ex.StackTrace, MessageBoxButton.OK, MessageBoxImage.Warning);
                    return false;
                }
            }

            MessageBox.Show("无此命令。", Globals.appName,
                MessageBoxButton.OK, MessageBoxImage.Error);
            return false;
        }
    }
}
